package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.enums.OaEnums;
import com.mxpio.erp.common.oa.entity.OaReimbursementSpecial;
import com.mxpio.erp.common.oa.entity.OaReimbursementSpecialLine;
import com.mxpio.erp.common.oa.service.OaReimbursementSpecialLineService;
import com.mxpio.erp.common.oa.service.OaReimbursementSpecialService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;
@Tag(name = "OaReimbursementSpecialLineController", description = "报销单-明细-项目-专用")
@RestController
@RequestMapping("/erp/oa/reim/special/line/")
public class OaReimbursementSpecialLineController {
    @Autowired
    private OaReimbursementSpecialService oaReimbursementSpecialService;

    @Autowired
    private OaReimbursementSpecialLineService oaReimbursementSpecialLineService;

    @GetMapping("list")
    @Operation(summary = "报销单-明细列表", description = "报销单-明细列表（无分页）", method = "GET")
    public Result<List<OaReimbursementSpecialLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<OaReimbursementSpecialLine> list = oaReimbursementSpecialLineService.list(OaReimbursementSpecialLine.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "报销单-明细列表", description = "报销单-明细列表（分页）", method = "GET")
    public Result<Page<OaReimbursementSpecialLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<OaReimbursementSpecialLine> pageResult = oaReimbursementSpecialLineService.listPage(OaReimbursementSpecialLine.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取报销单-明细", description = "根据ID获取报销单-明细", method = "GET")
    public Result<List<OaReimbursementSpecialLine>> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria criteria = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<OaReimbursementSpecialLine> list = oaReimbursementSpecialLineService.list(OaReimbursementSpecialLine.class, criteria);
        return Result.OK(list);
    }


//    @RequestMapping("add")
//    @Operation(summary = "保存报销表", description = "保存报销表")
//    public Result<OaReimbursementLine> add(@RequestBody OaReimbursementLine reimbursement) {
//        return  oaReimbursementLineService.save(reimbursement);
//    }

    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<OaReimbursementSpecialLine> edit(@RequestBody OaReimbursementSpecialLine reimbursement) {
        OaReimbursementSpecial byId = oaReimbursementSpecialService.getById(OaReimbursementSpecial.class,reimbursement.getBizNo());
        if (!byId.getOrderStatus().equals(OaEnums.OrderStatus.NEW.getCode())){
            return Result.error("单据新增状态下才可修改");
        }
        oaReimbursementSpecialLineService.update(reimbursement);
        return Result.OK(reimbursement);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<OaReimbursementSpecialLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            oaReimbursementSpecialLineService.delete(OaReimbursementSpecialLine.class, key);
        }
        return Result.OK();
    }
}
